package com.example.blog.controller;


import com.example.blog.constant.Constant;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.Result;
import com.example.blog.model.UserInfo;
import com.example.blog.service.UserService;
import com.example.blog.utils.JwtUtils;
import com.example.blog.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    UserService userService;


    @RequestMapping("/login")
    public Result login(String username, String password) {
        log.info("登录参数：{}，{}",username,password);
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            log.error("账号或密码为空");
            return Result.fail("账号或密码为空");
        }
        UserInfo userInfo = userService.getUserByName(username);
        if (userInfo == null) {
            log.error("账号不存在");
            return Result.fail("账号不存在");
        }
        if (!SecurityUtils.verify(password,userInfo.getPassword())) {
            log.error("密码错误");
            return Result.fail("密码错误");
        }
        Map<String,Object> claim = new HashMap<>();
        claim.put(Constant.TOKEN_ID,userInfo.getId());
        claim.put(Constant.TOKEN_USERNAME,userInfo.getUserName());
        claim.put(Constant.TOKEN_GITHUBURL,userInfo.getGithubUrl());
        String token = JwtUtils.genJwtToken(claim);
        log.info("token:{}",token);
        return Result.success(token);
    }


    //    @RequestMapping("/getLoginUserInfo")
    public Result getLoginUserInfo(HttpServletRequest request) {
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId == null || userId <= 0) {
            return Result.fail("用户不存在");
        }
        UserInfo userInfo =  userService.selectById(userId);
        if (userInfo == null) {
            return Result.fail("用户不存在");
        }
        return Result.success(userInfo);
    }

    //    @RequestMapping("/getAuthorInfo")
    public Result getAuthorInfo(Integer blogId) {
        log.info("接受到参数 blogId: {}",blogId);
        if (blogId < 1) return null;
        UserInfo userInfo = userService.getAuthorInfo(blogId);
        if (userInfo == null) {
            return Result.fail("无相关作者信息");
        }
        return Result.success(userInfo);
    }

    @RequestMapping("/getInfo")
    public Result getInfo(HttpServletRequest request, @RequestParam(required = false)Integer blogId) {
        log.info("接收参数 request：{} ，blogId：{}",request,blogId);
        if (blogId == null) {
            return getLoginUserInfo(request);
        }else {
            return getAuthorInfo(blogId);
        }
    }



    @RequestMapping("/register")
    public Result register(UserInfo userInfo) {
        UserInfo userInfo1 = userService.getUserByName(userInfo.getUserName());
        if (userInfo1 != null) {
            return Result.fail("用户名已被使用，请更换");
        }
        userInfo.setPassword(SecurityUtils.encrypt(userInfo.getPassword()));
        Integer result =  userService.insert(userInfo);
        if (result == 1) {
            return Result.success(null);
        }
        return Result.fail("注册失败，请联系管理员");
    }

}
